/**
 * Created by kurry on 2017/8/20.
 */
/**
 * Created by kurry on 2017/8/20.
 */
import React from 'react';
import {Layout, Tree, Input, Icon,Button} from 'antd';
const TreeNode = Tree.TreeNode;
import './less/type.less';
const {Header, Content} = Layout;
import 'antd/dist/antd.css';


//树形控件的参数
const x = 3;
const y = 2;
const z = 1;
const gData = [];

const generateData = (_level, _preKey, _tns) => {
  const preKey = _preKey || '0';
  const tns = _tns || gData;

  const children = [];
  for (let i = 0; i < x; i++) {
    const key = `${preKey}-${i}`;
    tns.push({title: key, key});
    if (i < y) {
      children.push(key);
    }
  }
  if (_level < 0) {
    return tns;
  }
  const level = _level - 1;
  children.forEach((key, index) => {
    tns[index].children = [];
    return generateData(level, key, tns[index].children);
  });
};
generateData(z);

class Type extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      gData,
      expandedKeys: ['0-0', '0-0-0', '0-0-0-0'],
      isShowWorkplace:false,//默认不显示工作组图标
      isInputDisabled:true,//默认输入框不可用
    }
  }

  componentWillMount() {

  }

  //拖拽节点
  onDragEnter = (info) => {
    console.log(info);
    // expandedKeys 需要受控时设置
    // this.setState({
    //   expandedKeys: info.expandedKeys,
    // });
  };
  onDrop = (info) => {
    console.log(info);
    const dropKey = info.node.props.eventKey;
    console.log(dropKey);
    const dragKey = info.dragNode.props.eventKey;
    console.log(dragKey);
    const dropPos = info.node.props.pos.split('-');
    console.log(dropPos);
    const dropPosition = info.dropPosition - Number(dropPos[dropPos.length - 1]);
    // const dragNodesKeys = info.dragNodesKeys;
    const loop = (data, key, callback) => {
      data.forEach((item, index, arr) => {
        if (item.key === key) {
          return callback(item, index, arr);
        }
        if (item.children) {
          return loop(item.children, key, callback);
        }
      });
    };
    const data = [...this.state.gData];
    let dragObj;
    loop(data, dragKey, (item, index, arr) => {
      arr.splice(index, 1);
      dragObj = item;
    });
    if (info.dropToGap) {
      let ar;
      let i;
      loop(data, dropKey, (item, index, arr) => {
        ar = arr;
        i = index;
      });
      if (dropPosition === -1) {
        ar.splice(i, 0, dragObj);
      } else {
        ar.splice(i - 1, 0, dragObj);
      }
    } else {
      loop(data, dropKey, (item) => {
        item.children = item.children || [];
        // where to insert 示例添加到尾部，可以是随意位置
        item.children.push(dragObj);
      });
    }
    this.setState({
      gData: data,
    });
  };


  render() {
    console.log(this.state.gData);
    const loop = data => data.map((item) => {
      if (item.children && item.children.length) {

        //节点的样式和组成：自定义样式、输入框等，便于修改和添加
        const itemTitle = <div className="type-tree-title" onMouseOver={()=>{
          console.log(item.key);
          this.setState({
            isShowWorkplace: true,
            targetTitleItemKey: item.key
          })
        }} onMouseOut={()=>{
          this.setState({
            isShowWorkplace: false,
            targetTitleItemKey: item.key
          })
        }}>
          <Input placeholder={item.key} disabled={this.state.isInputDisabled}/>
          <div id="workplace" style={{display:this.state.isShowWorkplace && item.key == this.state.targetTitleItemKey ? '' : 'none'}}>
          <span style={{display:this.state.isInputDisabled ? 'none':''}}><Button onClick={()=>{
            this.setState({
              isInputDisabled: true
            })
            console.log(item.key)
          }}>保存</Button></span>
            <span className="workplace-item-text" onClick={()=>{
              this.setState({
                isInputDisabled: false
              })
            }} style={{display:this.state.isInputDisabled ? '':'none'}}>修改 <Icon type="edit" /> </span>
            <span className="ant-divider"/>

            <span className="workplace-item-text">删除 <Icon type="minus-circle-o" /> </span>
            <span className="ant-divider"/>

            <span className="workplace-item-text">复制 <Icon type="plus-circle-o" /> </span>
          </div>

        </div>;

        return <TreeNode key={item.key} title={itemTitle}>{loop(item.children)}</TreeNode>;
      }

      //节点的样式和组成：自定义样式、输入框等，便于修改和添加
      const itemTitle = <div className="type-tree-title" onMouseOver={()=>{
        console.log(item.key);
        this.setState({
          isShowWorkplace: true,
          targetTitleItemKey: item.key
        })
      }} onMouseOut={()=>{
        this.setState({
          isShowWorkplace: false,
          targetTitleItemKey: item.key
        })
      }}>
        <Input placeholder={item.key} disabled={this.state.isInputDisabled}/>
        <div id="workplace" style={{display:this.state.isShowWorkplace && item.key == this.state.targetTitleItemKey ? '' : 'none'}}>
          <span style={{display:this.state.isInputDisabled ? 'none':''}}><Button onClick={()=>{
            this.setState({
              isInputDisabled: true
            })
            console.log(item.key)
          }}>保存</Button></span>
          <span className="workplace-item-text" onClick={()=>{
          this.setState({
            isInputDisabled: false
          })
          }} style={{display:this.state.isInputDisabled ? '':'none'}}>修改 <Icon type="edit" /> </span>
          <span className="ant-divider"/>

          <span className="workplace-item-text">删除 <Icon type="minus-circle-o" /> </span>
          <span className="ant-divider"/>

          <span className="workplace-item-text" onClick={()=>{

          }}>复制 <Icon type="plus-circle-o" /> </span>
        </div>

      </div>;
      return <TreeNode key={item.key} title={itemTitle}/>;
    });

    return (
      <div>
        <Header style={{background: '#fff', padding: '0px 80px'}} className="type-nav-parent">
          <span>类别面板</span>
        </Header>
        <Content style={{margin: '24px 16px 0'}}>
          <div style={{padding: 24, background: '#fff', minHeight: 360}}>
            <Tree
              className="draggable-tree"
              defaultExpandedKeys={this.state.expandedKeys}
              draggable
              onDragEnter={this.onDragEnter}
              onDrop={this.onDrop}
              showLine
            >
              {loop(this.state.gData)}
            </Tree>
          </div>
        </Content>
      </div>
    )
  }
}

export default Type;
